package com.olmarket.core.service;

import com.olmarket.core.dao.UserDAO;
import com.olmarket.core.entity.User;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author h'zh
 * @date: 12.14
 */

@Service
public class UserLoginService {

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private StringEncryptor encryptor;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public User getUser(String username, String password, int loginType) {
        if (username == null || password == null || "".equals(username) || "".equals(password)) return null;
        User user = userDAO.getUserByNameAndUserType(username, loginType);
        if (passwordEncoder.matches(password, user.getPassword())) {
            return user;
        }
        return null;
    }

    public boolean updateUserPassword(int userId, String newPassword) {
        return userDAO.updateUserPassword(userId, passwordEncoder.encode(newPassword)) > 0;
    }

}

